import {createRouter, createWebHashHistory} from 'vue-router'

const routes = [
	{
		path: '/',
		name: 'Home',
		component: ()=>import(/* webpackChunkName: "home" */ '@/views/home/home')
	},{
    path: '/cartList',
    name: 'CartList',
    component: ()=>import(/* webpackChunkName: "home" */ '@/views/cartList/CartList')
  },{
		path: '/Shop/:id',
		name: 'Shop',
		component: ()=>import(/* webpackChunkName: "shop" */ '@/views/Shop/Shop')
	},	{
		path: '/login',
		name: 'Login',
		component: ()=>import(/* webpackChunkName: "login" */ '@/views/login/login'),
		beforeEnter(to, from, next){
			const {isLogin} = localStorage;
			(isLogin) ? next({name: 'Home'}) : next()
		}
	},{
		path: '/register',
		name:'Register',
		component: ()=>import(/* webpackChunkName: "register" */ '@/views/Register/Register'),
		beforeEnter(to, from, next){
			const {isLogin} = localStorage;
			(isLogin) ? next({name: 'Home'}) : next()
		}
	}
]

const router = createRouter({
	history: createWebHashHistory(),
	routes
})

router.beforeEach((to, from, next) => {
	const {isLogin} = localStorage;
	const { name } = to;
	const isLoginOrRegister = (name === "Login" || name === "Register");
	(isLogin || isLoginOrRegister) ? next() : next({ name: 'Login'});
})

export default router
